import pytest
from playwright.sync_api import TimeoutError
import os
import allure
import inspect
from tool_class.handle_loguru import log
import traceback

project_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
def exception_handling(func):
    def inner(*args,**kwargs):
        page = args[0]
        try:
            log.info(f"执行操作:{func.__name__}")
            log.info(f"执行方法参数:{args[1:],kwargs}")
            res = func(*args,**kwargs)
            log.info(f"返回数据:{res}")
        except TimeoutError as e:
            log.error(f"元素超时未出现:{args[1:],kwargs}")
            log.error(e)
            log.error(traceback.format_exc())
            screenshot_path = os.path.join(project_path,'output_log/', 'screenshot.png')
            print(screenshot_path)
            # page.page.screenshot(path=screenshot_path)
            # # 将截图添加到 Allure 报告中
            # with open(screenshot_path, 'rb') as file:
            #     binary_data = file.read()
            #     allure.attach(binary_data, name='失败截图', attachment_type=allure.attachment_type.PNG)
            raise Exception(f"元素超时未出现:{args[1:],kwargs}")
        return res
    return inner

